package com.flink.flinkdemo.demo;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.streaming.api.datastream.*;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * java版本flink模型
 */
public class WordCountJavaSocket {
    public static void main(String[] args) throws Exception {
        String host ="localhost";
        if (args.length>0 && args[0].equals("win")){
            String arg = args[0];
            host="192.168.1.116";
        }

        //第一步：获取运行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //第二步：从文件中读取
//        DataStreamSource text = env.readTextFile("H:\\work-frame\\flink-d\\doc\\text.txt");
        DataStreamSource<String> text = env.socketTextStream(host, 9001, "\n");

        //第三步：计算数据
//        DataStream windowCount = text.flatMap(new FlatMapFunction<String, WordWithCount>() {
//            @Override
//            public void flatMap(String value, Collector out) throws Exception {
//                String[] splits = value.split("\\s");
//                for (String word : splits) {
//                    out.collect(new WordWithCount(word, 1L));
//                }
//            }
//        }).keyBy("word").timeWindow(Time.seconds(2),Time.seconds(1)).sum("count"); //.timeWindow(Time.seconds(2), Time.seconds(1))
        SingleOutputStreamOperator<WordWithCount> op = text.flatMap(new FlatMapFunction<String, WordWithCount>() {
            @Override
            public void flatMap(String value, Collector<WordWithCount> out) throws Exception {
                String[] split = value.split("\\s");
                for (String s : split) {
                    out.collect(new WordWithCount(s, 1L));
                }
            }
        });
        KeyedStream<WordWithCount, Tuple> k = op.keyBy("word");
        WindowedStream<WordWithCount, Tuple, TimeWindow> word = k.timeWindow(Time.seconds(1));
        SingleOutputStreamOperator<WordWithCount> windowCount = word.sum("count");
        //把数据打印到控制台
        DataStreamSink<WordWithCount> print = windowCount.print();//使用一个并行度
//        print.setParallelism()
//注意：因为flink是懒加载的，所以必须调用execute方法，上面的代码才会执行
        env.execute("streaming word count");
    }

    public static class WordWithCount {
        public String word;
        public long count;

        public WordWithCount() {
        }

        public WordWithCount(String word, long count) {
            this.word = word;
            this.count = count;
        }

        @Override
        public String toString() {
            return "WordWithCount{" +
                    "word='" + word + '\'' +
                    ", count=" + count +
                    '}';
        }
    }

}
